﻿/*----------------------------------------------------------------
// Copyright (C) 2009 
// 版权所有。 
//
// 文件名：
// 文件功能描述：部门信息管理
//
//
// 创建标识：
// 程序作者：翟允锋
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;

public partial class Department : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private DaDept dalDept = new DaDept();
    private DaEmployee dalEmp = new DaEmployee();
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            LoadDept2DropDownList();

            if (DDLdept.Items.Count > 0)
            {
                this.DDLdept_SelectedIndexChanged(null, null);
            }
            this.SetPanelVisiable(DetailPanel);
        }
    }



    private void LoadDept2DropDownList()
    {
        DataSet dsDept = dalDept.Query(string.Empty);

        Utility.BindDataSet2Combox(dsDept, DDLdept, "Dept_Name", "Dept_ID");
        Utility.BindDataSet2Combox(dsDept, CreateDep_Dept_ID, "Dept_Name", "Dept_ID");
        Utility.BindDataSet2Combox(dsDept, EditDep_Dept_ID, "Dept_Name", "Dept_ID");
        CreateDep_Dept_ID.Items.Insert(0, new ListItem("", ""));
        EditDep_Dept_ID.Items.Insert(0, new ListItem("", ""));
    }

    protected void DDLdept_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DisplayDetailInfo(DDLdept.SelectedValue);
        this.SetPanelVisiable(DetailPanel);
    }

    protected void SetPanelVisiable(Panel panel)
    {
        DetailPanel.Visible = false;
        CreatePanel.Visible = false;
        EditPanel.Visible = false;
        panel.Visible = true;
    }

    protected void DisplayDetailInfo(string strKey)
    {
        MoDept mod = dalDept.GetEntity(strKey);
        DetailDept_ID.Text = mod.Dept_Id;
        DetailDept_Name.Text = mod.Dept_Name;
        DetailDept_Tel.Text = mod.Dept_Tel;
        MoDept parent = dalDept.GetEntity(mod.Parent_Dept_Id);

        DetailDep_Dept_ID.Text = parent == null ? "" : parent.Dept_Name;
        DetailRem.Text = mod.Rem;
        DetailDept_Manager.Text = dalEmp.GetEmploreeInfo(mod.Dept_Manager_Code).Fullname;
    }

    protected void DisplayEditInfo(string strKey)
    {
        MoDept mod = dalDept.GetEntity(strKey);
        EditDept_ID.Text = mod.Dept_Id;
        EditDept_Name.Text = mod.Dept_Name;
        EditDep_Dept_ID.SelectedValue = mod.Parent_Dept_Id;
        EditDept_Tel.Text = mod.Dept_Tel;
        EditRem.Text = mod.Rem;

        EditDept_Manager_Code.Items.Clear();
        Utility.BindDataSet2Combox(dalEmp.GetEmploreeByDeptID(strKey), EditDept_Manager_Code, "FullName", "Emp_ID");
        EditDept_Manager_Code.Items.Insert(0, new ListItem("", ""));

        EditDept_Manager_Code.SelectedValue = mod.Dept_Manager_Code;
    }

    protected void EditGo_Click(object sender, EventArgs e)
    {
        this.DisplayEditInfo(DDLdept.SelectedValue);
        this.SetPanelVisiable(EditPanel);
    }

    protected void EditBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(DetailPanel);
    }

    protected void DeleteGo_Click(object sender, EventArgs e)
    {
        try
        {
            int rtn = dalDept.Delete(DetailDept_ID.Text);
            if (rtn == 1)
            {
                if (DDLdept.Items.Count > 0)
                {
                    this.LoadDept2DropDownList();
                    this.DisplayDetailInfo(DDLdept.SelectedValue);
                }
                else
                {
                    this.DDLdept_SelectedIndexChanged(null, null);
                }

                OperationResult.Text = "部门信息删除成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "部门信息删除失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("部门信息删除失败", ex);
            OperationResult.Text = "部门信息删除失败！"; divResult.Visible = true;
        }
    }

    protected void CreateGo_Click(object sender, EventArgs e)
    {
        this.CreateCancel();
        this.SetPanelVisiable(CreatePanel);
    }

    protected void CreateBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(DetailPanel);
    }

    protected void EditSave_Click(object sender, EventArgs e)
    {
        MoDept mod = new MoDept();

        try
        {
            mod.Dept_Id = EditDept_ID.Text;
            mod.Dept_Manager_Code = EditDept_Manager_Code.SelectedValue == "" ? null  : EditDept_Manager_Code.SelectedValue;
            mod.Parent_Dept_Id = EditDep_Dept_ID.SelectedValue == "" ? null : EditDep_Dept_ID.SelectedValue;
            mod.Dept_Name = EditDept_Name.Text.Trim();
            mod.Dept_Tel = EditDept_Tel.Text.Trim();
            mod.Rem = EditRem.Text.Trim();

            int rtn = dalDept.Update(mod);

            if (rtn == 1)
            {
                this.LoadDept2DropDownList();
                OperationResult.Text = "部门信息修改成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "部门信息修改失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("部门信息修改失败", ex);
            OperationResult.Text = "部门信息修改失败！"; divResult.Visible = true;
        }
    }

    protected void CreateSave_Click(object sender, EventArgs e)
    {
        MoDept mod = new MoDept();

        mod.Dept_Manager_Code = CreateDept_Manager_Code.SelectedValue;
        mod.Dept_Name = CreateDept_Name.Text.Trim();
        mod.Dept_Tel = CreateDept_Tel.Text.Trim();
        mod.Parent_Dept_Id = CreateDep_Dept_ID.SelectedValue;
        mod.Rem = CreateRem.Text.Trim();

        try
        {
            int rtn = dalDept.CreateDeptartment(mod);

            if (rtn == 1)
            {
                this.LoadDept2DropDownList();
                OperationResult.Text = "部门信息创建成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "部门信息创建失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("部门信息创建失败", ex);
            OperationResult.Text = "部门信息创建失败！"; divResult.Visible = true;
        }
    }

    protected void CreateCancel()
    {
        CreateRem.Text = "";
        CreateDept_Name.Text = "";
        CreateDept_Tel.Text = "";
        CreateDep_Dept_ID.SelectedIndex = 0;
    }

    protected void EditDep_Dept_ID_SelectedIndexChanged(object sender, EventArgs e)
    {
        EditDept_Manager_Code.Items.Clear();

        string deptid = EditDep_Dept_ID.Text;
        if (!string.IsNullOrEmpty(deptid))
        {
            Utility.BindDataSet2Combox(dalEmp.GetEmploreeByDeptID(deptid), EditDept_Manager_Code, "FullName", "Emp_Id");
        }
        EditDept_Manager_Code.Items.Insert(0, new ListItem("", ""));
    }

    protected void CreateDep_Dept_ID_SelectedIndexChanged(object sender, EventArgs e)
    {
        CreateDept_Manager_Code.Items.Clear();

        string deptid = CreateDep_Dept_ID.SelectedValue;
        if (!string.IsNullOrEmpty(deptid))
        {
            Utility.BindDataSet2Combox(dalEmp.GetEmploreeByDeptID(deptid), CreateDept_Manager_Code, "FullName", "Emp_Id");
        }
        CreateDept_Manager_Code.Items.Insert(0, new ListItem("", ""));
    }
}
